def outer_function():
    # 外部函数的变量
    y = 10
    def inner_function(z):
        nonlocal y #外层（非全局）作用域的变量。
        y += 1
        # 内部函数引用外部函数的变量
        return f"  {y + z} "
    
    return inner_function  # 返回内部函数

# 使用闭包
closure = outer_function()
print(closure(5)) 
print(closure(5))

# 定义一个装饰器
def logger(func):
    def wrapper(*args, **kwargs):
        print(f"调用函数: {func.__name__}")
        result = func(*args, **kwargs)
        print(f"函数 {func.__name__} 执行完成")
        return result
    return wrapper

def bank_account(initial_balance=0):
    balance = initial_balance
    
    def deposit(amount):
        nonlocal balance
        balance += amount
        return balance
    
    def withdraw(amount):
        nonlocal balance
        if amount > balance:
            raise ValueError("余额不足")
        balance -= amount
        return balance
    
    def get_balance():
        return balance
    
    return deposit, withdraw, get_balance